package lyj.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lyj.springcloud.entities.CommonResult;
import lyj.springcloud.myfallbackhandler.MyFallbackHandler;
import lyj.springcloud.myselfblockhandler.CustomBlockHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 自定义服务降级及异常时的兜底方法返回信息
 */
@RestController
public class RateLimitController {

    @GetMapping("/ratelimit/{id}")
    @SentinelResource(value = "ratelimit",blockHandler = "myselfDegradeMethod1")
    public CommonResult associationA(@PathVariable("id") String id) {
        return new CommonResult();
    }

//    ------------------------------------------------------------------------------------------------
    /**
     * 降级方法返回值必须与原方法一致；参数必须一致且包含BlockException参数
     * @param id
     * @param e
     * @return
     */
    public CommonResult myselfDegradeMethod1(String id, BlockException e) {
        return new CommonResult("id：" + id +"服务异常:" + e.getMessage());
    }


    /**
     * 测试超出sentinel配置规则
     *     全局服务兜底方法
     *
     * @param id
     * @return
     */
    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomBlockHandler.class,
            blockHandler = "myselfHandle2") // 指定调用的方法
    public CommonResult customerBlockHandler(String id)
    {
        return new CommonResult("200","按客戶自定义限流处理 id : " + id);
    }


//    ------------------------------------------------------------------------------------------------
    /**
     * fallback：测试异常兜底方法
     * @param id
     * @return
     */
    @GetMapping("/rateLimit/customerFallBack")
    @SentinelResource(value = "customerFallBack",
            blockHandlerClass = CustomBlockHandler.class,
            blockHandler = "myselfHandle2",// 指定调用的方法
            fallback = "fall_back_method"
    )
    public CommonResult customerFallBack(String id)
    {
        try {
            int i= 1/0;
        }catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return new CommonResult("200","按客戶自定义限流处理 id : " + id);
    }


    /**
     * 抛出异常的时候提供 fallback 处理逻辑
     * @return
     */
    public CommonResult fall_back_method(String id,Throwable throwable) {
        return new CommonResult("200","服务异常，请稍后重试:" + throwable.getMessage());
    }

//    ------------------------------------------------------------------------------------------------

    /**
     * fallbackClass：全局异常兜底方法
     * @param id
     * @return
     */
    @GetMapping("/rateLimit/globalCustomerFallBack")
    @SentinelResource(value = "globalCustomerFallBack",
            blockHandlerClass = CustomBlockHandler.class,
            blockHandler = "myselfHandle2",// 指定调用的方法
            fallback = "fall_back_method1",
            fallbackClass = MyFallbackHandler.class
    )
    public CommonResult globalCustomerFallBack(String id)
    {
        try {
            int i= 1/0;
        }catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return new CommonResult("200","按客戶自定义限流处理 id : " + id);
    }

}
